package com.lfnyxy.service.org.imp;

import com.lfnyxy.common.StringTool;
import com.lfnyxy.dao.org.SysUserDao;
import com.lfnyxy.eenum.SysUserStatus;
import com.lfnyxy.entity.org.SysUserEntity;
import com.lfnyxy.query.org.SysUserQuery;
import com.lfnyxy.service.org.SysUserService;
import com.lfnyxy.util.MD5Service;
import com.lfnyxy.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImp implements SysUserService {
    @Autowired
    public SysUserDao sysUserDao;

    public int CreateItem(SysUserEntity entity) {
        if (entity != null) {
            if (CheckIsExist(entity)) {
                return -1;
            }

            String salt = UUIDUtil.uuid();
            entity.setSalt(salt);

            String mPass = MD5Service.formPassToDbPass(entity.getPassword(), salt);
            entity.setPassword(mPass);

            entity.setStatus(SysUserStatus.ACTICE.getCode());

            return sysUserDao.CreateItem(entity);
        } else {
            return 0;
        }
    }

    public int UpdateItem(SysUserEntity entity) {
        if (entity != null) {
            if (CheckIsExist(entity)) {
                return -1;
            }
            return sysUserDao.UpdateItem(entity);
        } else {
            return 0;
        }
    }

    public List<SysUserEntity> QueryPageList(SysUserQuery query) {
        int count = sysUserDao.QueryPageListCount(query);
        query.setTotal(count);
        if (count > 0) {
            return sysUserDao.QueryPageList(query);
        }
        return null;
    }

    public List<SysUserEntity> QueryAll() {
        return sysUserDao.QueryAll();
    }

    public SysUserEntity QueryItem(int Id) {
        if (Id > 0) {
            return sysUserDao.QueryItem(Id);
        } else {
            return null;
        }
    }

    public SysUserEntity QueryItemByAccount(String account) {
        if (StringTool.isNotEmpty(account)) {
            return sysUserDao.QueryItemByAccount(account);
        } else {
            return null;
        }
    }

    public int DeleteItem(int Id) {
        if (Id > 0) {
            return sysUserDao.DeleteItem(Id);
        } else {
            return 0;
        }
    }

    private boolean CheckIsExist(SysUserEntity entity) {
        if (entity != null) {
            return sysUserDao.CheckIsExist(entity) > 0;
        } else {
            return false;
        }
    }
}